package com.lock.demo.mysql.test;

import com.lock.demo.config.ApplicationContextProvider;
import com.lock.demo.mysql.lock.DBLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ProjectName: lock_demo
 * @Package: com.lock.demo.mysql.test
 * @Author: huat
 * @Date: 2021/5/4 15:37
 * @Version: 1.0
 */
@Slf4j
public class StockService implements Runnable{

    private DBLock dbLock;

    public StockService() {
        dbLock= ApplicationContextProvider.getBean(DBLock.class);
    }

    @Override
        public void run() {
        boolean flag=false;
        //        防止代码报错，导致死锁
            try {
                dbLock.lock();
                 flag=new Stock().reduceStock();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                dbLock.unlock();
            }

            if(flag){
                log.info(Thread.currentThread().getName()+"减少成功");
            }else{
                log.info(Thread.currentThread().getName()+"减少失败");
            }
        }


}
